<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="$t('msg.user.'+item.name)">
        <el-input v-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.value"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{$t('msg.common.search')}}</el-button>
        <el-button @click="onReset">{{$t('msg.common.reset')}}</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="userName" label="账号" width="180" />
      <el-table-column prop="tel" label="电话" width="120" />
      <el-table-column prop="userInfo.name" label="姓名" width="120" />
      <el-table-column prop="userInfo.nikeName" label="昵称" width="120" />
      <el-table-column prop="userInfo.headPic" label="头像" width="120" />
      <el-table-column prop="userInfo.age" label="年龄" width="120" />
      <el-table-column prop="userInfo.sex" label="性别" width="120" />
      <el-table-column prop="userInfo.birthDate" label="生日" width="160" />
      <el-table-column prop="userInfo.cardType" label="证件类型" width="120" />
      <el-table-column prop="userInfo.cardCode" label="证件号" width="120" />
      <el-table-column fixed="right" label="操作" width="160">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
          <el-button  text size="small" @click="handleClick(scope.row)">编辑</el-button>
           <el-popconfirm title="确定要删除吗?" @confirm="handleDelete(scope.row)">
            <template #reference>
              <el-button  text type="danger" size="small">删除</el-button>
            </template>
           </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <user-form :dialogTableVisible ="dialogVisible"></user-form>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { list, deleteById } from '@/api/common/common'
import { dateToString, dateToNext, dateTimeToString } from '@/utils/date'
import { singleUrl, arrayUrl } from '@/utils/file'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'
import userForm from '../form/index.vue'

const tableDatas = ref({})
const dialogVisible = ref(false)
const lostRef = ref(null)

const loading = ref(false)

const i18n = useI18n()

const handleClose = () => {
  dialogVisible.value = false
}
const searchPage = ref({
  items: [
    {
      name: 'sex',
      itemType: 'STRING',
      sign: '==',
      value: ''
    },
    {
      name: 'name',
      itemType: 'STRING',
      sign: 'like',
      value: ''
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const tableData = ref([])

const lostEntity = ref({})

const searchFunction = async () => {
  list(searchPage.value, 'user').then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'code',
        itemType: 'STRING',
        sign: '==',
        value: ''
      },
      {
        name: 'name',
        itemType: 'STRING',
        sign: 'like',
        value: ''
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const handleClick = row => {
  dialogVisible.value = true
  lostEntity.value = row
  lostEntity.value.provideDateTime = dateTimeToString(0)
}

const handleDelete = row => {
  loading.value = true
  deleteById(row.id, 'user').then(res => {
    loading.value = false
    ElMessage.success('成功')
    searchFunction()
  }).catch(err => {
    loading.value = false
    ElMessage.error(err.message)
  })
}
</script>

<style scope>
.search-container {
  width: 100%;
  height: 100px;
}
</style>
